package com.cyan.note.mapper;

import com.cyan.note.entity.Shop;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 店铺数据访问层
 * 严格按照超级管理员权限设计 - 只能访问系统级基础信息
 * 
 * @author ZhangzhenYu
 * @since 2025-01-14
 */
@Mapper
public interface ShopMapper {
    
    /**
     * 根据ID查找店铺基础信息（仅超级管理员可访问的字段）
     */
    @Select("SELECT id, name, code, description, status, contact_phone, " +
            "created_by, created_at, updated_at " +
            "FROM shops WHERE id = #{id}")
    Shop selectById(Long id);
    
    /**
     * 查询所有店铺基础信息列表（仅超级管理员可访问的字段）
     */
    @Select("SELECT id, name, code, description, status, contact_phone, " +
            "created_by, created_at, updated_at " +
            "FROM shops ORDER BY created_at DESC")
    List<Shop> selectAllBasicInfo();
    
    /**
     * 根据状态查询店铺列表
     */
    @Select("SELECT id, name, code, description, status, contact_phone, " +
            "created_by, created_at, updated_at " +
            "FROM shops WHERE status = #{status} ORDER BY created_at DESC")
    List<Shop> selectByStatus(String status);
    
    /**
     * 创建新店铺（仅基础信息）
     */
    @Insert("INSERT INTO shops (name, code, description, contact_phone, status, created_by) " +
            "VALUES (#{name}, #{code}, #{description}, #{contactPhone}, #{status}, #{createdBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Shop shop);
    
    /**
     * 更新店铺基础信息（仅超级管理员可修改的字段）
     */
    @Update("UPDATE shops SET name = #{name}, description = #{description}, " +
            "contact_phone = #{contactPhone}, updated_at = CURRENT_TIMESTAMP " +
            "WHERE id = #{id}")
    int updateBasicInfo(Shop shop);
    
    /**
     * 更新店铺状态
     */
    @Update("UPDATE shops SET status = #{status}, updated_at = CURRENT_TIMESTAMP " +
            "WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status);
    
    /**
     * 删除店铺（系统级删除）
     */
    @Delete("DELETE FROM shops WHERE id = #{id}")
    int deleteById(Long id);
    
    /**
     * 统计店铺总数（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM shops")
    long countTotal();
    
    /**
     * 按状态统计店铺数量（系统监控用）
     */
    @Select("SELECT COUNT(*) FROM shops WHERE status = #{status}")
    long countByStatus(String status);
    
    /**
     * 检查店铺编号是否存在
     */
    @Select("SELECT COUNT(*) FROM shops WHERE code = #{code}")
    long countByCode(String code);
    
    /**
     * 检查店铺编号是否存在（排除指定ID）
     */
    @Select("SELECT COUNT(*) FROM shops WHERE code = #{code} AND id != #{excludeId}")
    long countByCodeExcludeId(@Param("code") String code, @Param("excludeId") Long excludeId);
}